<?php
namespace app\common\process;


class ProductInfo extends Info {
	protected $model = 'tysj\\Product';
	protected $with = [];
	protected $append = ['photos_url', 'info_photos_url', 'ost_sale', 'store_name'];
	protected $updateInventoryScene = 'update_inventory';

	public function list($fun = null) {
		$myFun = function($model) use ($fun) {
			$search = loader()->loadAny('search', 'tysj\\Product');
			if($search) { //是否有搜索条件生成器
				$search->list($model); //生成搜索条件
			}
			if(is_callable($fun)) {
	            $fun($model);
	        }
		};
		return parent::list($myFun);
	}

	public function get_save_data() {
		return loader()->loadAny('request', 'tysj\\Product')->save_data();
	}

	//保存图片相关信息
	public function after_save($model, $data, $pid) {
		$this->save_photos($model, $data, $pid);
		$this->save_specs($model, $data, $pid);
		$this->save_info_photo($model, $data, $pid);
		$this->save_native_product($model, $data, $pid);
		$this->save_group_buying($model, $data, $pid);
	}

	//保存产品图片
	public function save_photos($model, $data, $pid) {
		$enum = loadEnum('ProductImageType');
		$prductImageModel = loadModel('tysj\\ProductImageNexus');
		$type = $enum->code2key('pic');
		$photos = $data['photos'];
		$datas = [];
		$prductImageModel->where(['pid' => $pid, 'type' => $type])->delete();
		foreach ($photos as $k => $iid) {
			$datas[] = [
				'pid'  => $pid,
				'iid'  => $iid,
				'type' => $type
			];
		}
		$prductImageModel->insertAll($datas);
	}

	//保存规格
	public function save_specs($model, $data, $pid) {
		$productSpecsModel = loadModel('tysj\\ProductSpecsNexus');
		$specs = $data['specs'];
		$datas = [];
		$productSpecsModel->where(['pid' => $pid])->delete();
		foreach ($specs as $k => $psid) {
			$datas[] = [
				'pid'  => $pid,
				'psid' => $psid
			];
		}
		$productSpecsModel->insertAll($datas);
	}

	//获取私密列表,不需要验证
    public function private_list($user, $fun = null) {
        $model = $this->model;
        $validate = $this->validate;

        $post = loader()->loadAny('request', 'tysj\\Product')->private_list();

        if (!$validate->scene($this->privateListScene)->check($post)) {
            $this->error($validate->getError());
        }

        $search = loader()->loadAny('search', 'tysj\\Product');
		if($search) { //是否有搜索条件生成器
			$search->private_list($model, $post); //生成搜索条件
		}

        if(is_callable($fun)) {
            $fun($model);
        }
        if($post['limit']) {
            $list = $model->paginate($post['limit']);
        } else {
            $list = $model->select();
        }
        $this->assign('list', $list);
        return $this->fetch();
    }

	//保存产品详情图片
	public function save_info_photo($model, $data, $pid) {
		$enum = loadEnum('ProductImageType');
		$prductImageModel = loadModel('tysj\\ProductImageNexus');
		$type = $enum->code2key('info');
		$photos = $data['info_photo'];
		$datas = [];
		$prductImageModel->where(['pid' => $pid, 'type' => $type])->delete();
		foreach ($photos as $k => $iid) {
			$datas[] = [
				'pid'  => $pid,
				'iid'  => $iid,
				'type' => $type
			];
		}
		$prductImageModel->insertAll($datas);
	}

	//保存土特产专区信息
	public function save_native_product($model, $data, $pid) {
		$model = loadModel('tysj\\NativeProduct');
		if($data['area_id']) {
			$row = $model->where(['pid' => $pid])->find();
			if($row) {
				$row->areaid = $data['area_id'];
				$row->save();
			} else {
				$model->save([
					'pid'    => $pid,
					'areaid' => $data['area_id']
				]);
			}
			
		} else {
			$model->where(['pid' => $pid])->delete();
		}
	}

	//保存团购专区信息
	public function save_group_buying($model, $data, $pid) {
		$model = loadModel('tysj\\ProductGroupBuying');
		if($data['is_group_buying']) {
			$row = $model->where(['pid' => $pid])->find();
			if(!$row) {
				$model->save([
					'pid'    => $pid
				]);
			}
		} else {
			$model->where(['pid' => $pid])->delete();
		}
	}
}